package cn.com.ejiakanghu.service;

import cn.com.ejiakanghu.config.OrderSelfTypeEnum;
import cn.com.ejiakanghu.core.exceptions.BizException;
import cn.com.ejiakanghu.core.utils.RespUtil;
import cn.com.ejiakanghu.core.vo.BaseResult;
import cn.com.ejiakanghu.task.OrderUnpaidTask;
import cn.com.ejiakanghu.task.OrderUnconfirmTask;
import cn.com.ejiakanghu.vo.SelfConfirmVo;
import cn.ejiakanghu.logistics.service.impl.LogisticBusiBaseServiceImpl;
import cn.ejiakanghu.logistics.service.impl.OrderStateService;
import cn.ejiakanghu.logistics.task.OrderSelfTask;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.binarywang.wxpay.bean.payscore.WxPayScoreResult;
import org.apache.commons.lang3.StringUtils;
import org.linlinjava.litemall.core.config.ExceptionMsgEnum;
import org.linlinjava.litemall.core.lock_rfid.LockRfidUtil;
import org.linlinjava.litemall.core.lock_rfid.UnLockCabinetGoodsVo;
import org.linlinjava.litemall.core.lock_rfid.UnLockGoodsVo;
import org.linlinjava.litemall.core.task.TaskService;
import org.linlinjava.litemall.core.tool.wxpayscore.WxPayScoreTool;
import org.linlinjava.litemall.core.util.AgedmallException;
import org.linlinjava.litemall.core.util.InventoryUtil;
import org.linlinjava.litemall.core.util.JacksonUtil;
import org.linlinjava.litemall.db.dao.*;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.AgedmallWxNoDepositService;
import org.linlinjava.litemall.db.service.LitemallGoodsProductService;
import org.linlinjava.litemall.db.service.LitemallOrderGoodsService;
import org.linlinjava.litemall.db.service.LitemallOrderService;
import org.linlinjava.litemall.db.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.linlinjava.litemall.core.config.ExceptionMsgEnum.ORDER_CONFIRM_RECEIVE_SELF_BASE_ID_NULL;


@Service
public class OrderService {
    private Logger logger = LoggerFactory.getLogger(OrderService.class);

    @Autowired
    private LitemallOrderService litemallOrderService;
    @Autowired
    private AgedmallServiceOrderMapper agedmallServiceOrderMapper;
    @Autowired
    private LitemallOrderGoodsService litemallOrderGoodsService;
    @Autowired
    private LitemallGoodsProductService litemallGoodsProductService;
    @Autowired
    private AgedmallHospitalSendSelfLocationMapper agedmallHospitalSendSelfLocationMapper;
    @Autowired
    private LockRfidUtil lockRfidUtil;
    @Autowired
    private TaskService taskService;
    @Autowired
    private AgedmallWxNoDepositService agedmallWxNoDepositService;
    @Autowired
    private WxPayScoreTool wxPayScoreTool;
    @Resource
    private LogisticBusiBaseServiceImpl logisticBusiBaseServiceImpl;
    @Autowired
    private AgedmallLgBaseInfoMapper agedmallLgBaseInfoMapper;
    @Autowired
    private LitemallOrderGoodsMapper litemallOrderGoodsMapper;
    @Autowired
    private OrderStateService orderStateService;
    @Autowired
    private LitemallOrderItemMapper litemallOrderItemMapper;
    @Autowired
    private AgedmallLgRfidGoodsCurStatMapper agedmallLgRfidGoodsCurStatMapper;
    @Autowired
    private AgedmallGoodsEquMapper agedmallGoodsEquMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private AgedmallLgMissionMapper agedmallLgMissionMapper;

    /**
     * 根据单号后去订单并执行取消订单
     * @param bigOrderCode
     * @param reason
     * @return
     */
    public BaseResult cancel(String bigOrderCode, String reason) {
        LitemallOrder order = litemallOrderService.findBySn(bigOrderCode);
        return cancel(order, reason);
    }

    /**
     * 执行取消订单业务
     * @param order
     * @param reason
     * @return
     */
    public BaseResult cancel(LitemallOrder order, String reason) {
        if (null == order) {
            return RespUtil.badArgumentValue();
        }
        String bigOrderCode = order.getOrderSn();
        Integer userId = order.getUserId();
        OrderHandleOption orderHandleOption = OrderUtil.build(order);
        if (!orderHandleOption.isCancel()) {
            throw new BizException("订单不能取消");
        }
        order.setOrderStatus(OrderUtil.STATUS_CANCEL);
        order.setCancelReason(reason);
        order.setUpdateTime(LocalDateTime.now());
        litemallOrderService.update(order);

        //判断是否未免押订单，免押订单需要取消免押状态
        cancelWxNoDepositOrder(bigOrderCode, reason);

        // 更新服务订单
        AgedmallServiceOrderExample serviceOrderExample = new AgedmallServiceOrderExample();
        serviceOrderExample.createCriteria().andDeletedEqualTo(false)
                .andOrderIdEqualTo(order.getId().longValue());
        AgedmallServiceOrder serviceOrder = new AgedmallServiceOrder();
        serviceOrder.setStat(ServiceOrderConstant.STATUS_CANCEL);
        agedmallServiceOrderMapper.updateByExampleSelective(serviceOrder, serviceOrderExample);

        List<LitemallOrderItem> orderItemList = litemallOrderService.getOrderItemListByBigOrderCode(userId, bigOrderCode);
        for (LitemallOrderItem orderItem : orderItemList) {
            // 更新子订单状态
            orderItem.setOrderStatus(OrderUtil.STATUS_CANCEL);
            litemallOrderService.updateItem(orderItem);
            String childOrderCode = orderItem.getChildOrderCode();
            List<LitemallOrderGoods> orderGoodsList = litemallOrderGoodsService.getOrderGoodsListByChildOrderCode(childOrderCode);
            for (LitemallOrderGoods orderGoods : orderGoodsList) {
                // 还原库存数量 服务没有库存概念
                if (!OrderGoodsConstant.TYPE_EXTEND.equals(orderGoods.getType())) {
                    Integer productId = orderGoods.getProductId();
                    Short number = orderGoods.getNumber();
                    litemallGoodsProductService.addStock(productId, number);
                }
                if (StringUtils.isNotEmpty(orderGoods.getRfid())) {
                    List<String> rfidList = Arrays.asList(orderGoods.getRfid().split(","));
                    // 取消rfid锁定
                    if (InventoryUtil.STOCK_SOFTCHAIN.equals(orderGoods.getStockType())) {
                        UnLockGoodsVo unLockGoodsVo = new UnLockGoodsVo();
                        unLockGoodsVo.setRfid(rfidList);
                        int type=0;
                        //        如果为到院自提
                        AgedmallHospitalSendSelfLocation selfLocation = null;
                        if(orderGoods.getHospitalSelfLocationId()!=null){
                            selfLocation = agedmallHospitalSendSelfLocationMapper.selectByPrimaryKey(orderGoods.getHospitalSelfLocationId());
                        }
                        if(ObjectUtil.isNotNull(selfLocation)&&selfLocation.getInventoryFlag()==0){
                            type=2;
                        }
                        unLockGoodsVo.setType(type);
                        lockRfidUtil.unlockGoods(unLockGoodsVo);
                    }
                    if (InventoryUtil.STOCK_CABINET.equals(orderGoods.getStockType())) {
                        UnLockCabinetGoodsVo unLockCabinetGoodsVo = new UnLockCabinetGoodsVo();
                        unLockCabinetGoodsVo.setRfid(rfidList);
                        lockRfidUtil.unlockCabinetGoods(unLockCabinetGoodsVo);
                    }
                }
            }
        }

        // 取消订单超时未支付任务
        taskService.removeTask(new OrderUnpaidTask(order.getId()));
        return RespUtil.success();
    }

    /**
     * 取消订单 判断用户是否登录
     * @param userId
     * @param bigOrderCode
     * @param reason
     * @return
     */
    public BaseResult cancel(Integer userId, String bigOrderCode, String reason) {
        if (userId == null) {
            return RespUtil.unlogin();
        }
        LitemallOrder order = litemallOrderService.findBySn(userId, bigOrderCode);
        return cancel(order, reason);
    }

    public void cancelWxNoDepositOrder(String bigOrderCode, String reason) {
        //选择取消订单，免押订单进行免押取消
        long count = agedmallWxNoDepositService.getOrderIsNoDepositByBigOrderCode(bigOrderCode);
        if(count > 0) {
            //取消免押订单
            BaseResult result = wxPayScoreTool.payScoreCancel(bigOrderCode, StringUtils.isNotBlank(reason) ? reason : "管理员取消订单");
            WxPayScoreResult wxPayScoreResult = (WxPayScoreResult) result.getData();
            logger.debug("管理员取消订单：{},{}", bigOrderCode, JacksonUtil.gson.toJson(wxPayScoreResult));
            if(wxPayScoreResult != null) {
                String state = wxPayScoreResult.getState();
                agedmallWxNoDepositService.updateNoDepositStat(bigOrderCode, state);
            }
        }
    }

    /**
     * 根据子订单号取消免押订单
     * @param childOrderCode
     * @param reason
     */
    public void cancelWxNoDepositByChildOrderCode(String childOrderCode, String reason) {
        //选择取消订单，免押订单进行免押取消
        String bigOrderCode = agedmallWxNoDepositService.getOrderIsNoDepositByChildOrderCode(childOrderCode);
        if(StringUtils.isNotBlank(bigOrderCode)) {
            //取消免押订单
            BaseResult result = wxPayScoreTool.payScoreCancel(bigOrderCode, StringUtils.isNotBlank(reason) ? reason : "管理员取消订单");
            WxPayScoreResult wxPayScoreResult = (WxPayScoreResult) result.getData();
            logger.debug("管理员取消订单：{},{}", bigOrderCode, JacksonUtil.gson.toJson(wxPayScoreResult));
            if(wxPayScoreResult != null) {
                String state = wxPayScoreResult.getState();
                agedmallWxNoDepositService.updateNoDepositStat(bigOrderCode, state);
            }
        }
    }

    /**
     * 确认收货
     * @param orderItem
     * @param isSelf
     * @return
     */
    public BaseResult confirmReceiptNormal(LitemallOrderItem orderItem, boolean isSelf) {
        //获取次级订单的信息
        if (null == orderItem) {
            return RespUtil.badArgumentValue();
        }

        Integer userId = orderItem.getUserId();
        String bigOrderCode = orderItem.getOrderCode();
        String childOrderCode = orderItem.getChildOrderCode();

        //获取订单状态       当status == 301 || status == 302才能确认收货
        OrderHandleOption orderHandleOption = OrderUtil.buildItem(orderItem);
        //判断物流是否完成 未完成不能收货
        boolean finishSubOrderLogistics = isFinishSubOrderLogistics(bigOrderCode, orderItem.getId());
        //isConfirm 为true才能确认收货
        if (!orderHandleOption.isConfirm()) {
            throw new BizException("订单不能确认收货");
        }
        if (!finishSubOrderLogistics) {
            //结束配送任务
            AgedmallLgBaseInfoExample baseInfoExample = new AgedmallLgBaseInfoExample();
            baseInfoExample.or().andOrderCodeEqualTo(bigOrderCode);
            List<AgedmallLgBaseInfo> agedmallLgBaseInfos = agedmallLgBaseInfoMapper.selectByExampleWithBLOBs(baseInfoExample);
            AgedmallLgBaseInfo baseInfo = agedmallLgBaseInfos.size() > 0 ? agedmallLgBaseInfos.get(0) : null;
            if(baseInfo != null) {
                logger.debug("点击确认收货，配送中状态更改为结束 {}，{}", bigOrderCode, isSelf);
                AgedmallLgBaseInfo lgBaseInfo = new AgedmallLgBaseInfo();
                lgBaseInfo.setId(baseInfo.getId());
                lgBaseInfo.setLgStat(LgBaseInfoIsOverEnum.END.getCode());
                agedmallLgBaseInfoMapper.updateByPrimaryKeySelective(lgBaseInfo);
            }
        }
        //租赁状态 更新订单状态
        if (GoodsConstant.GOODS_TYPE_LEASE.equals(orderItem.getGoodsType())) {
            orderItem.setOrderStatus(OrderUtil.STATUS_LEASE_CONFIRM);
        } else {
            //更新订单状态 已收货状态
            orderItem.setOrderStatus(OrderUtil.STATUS_CONFIRM);
            if (isSelf) {
                orderItem.setShipArriveTime(LocalDateTime.now());
                //取消自提定时任务
                taskService.removeTask(new OrderSelfTask(orderItem.getId()));
            }
            litemallOrderService.saveSettle(orderItem, null);
            litemallOrderService.saveDeduct(orderItem);
            //更新完成时间
            orderItem.setFinishTime(LocalDateTime.now());
            // 取消订单超时未确认任务
            taskService.removeTask(new OrderUnconfirmTask(orderItem.getId()));
        }

        orderItem.setConfirmTime(LocalDateTime.now());
        litemallOrderService.updateItem(orderItem);
        //判断是否有商品，更新rfid状态
        orderStateService.confirmOrderRfidStateChange(orderItem);
        //更新签收表
        logisticBusiBaseServiceImpl.insertSignInConfirm(bigOrderCode, childOrderCode, userId);
        return RespUtil.success();
    }

    /**
     * 送货上门确认收货
     * @param userId
     * @param bigOrderCode
     * @param childOrderCode
     * @param isSelf
     * @return
     */
    public BaseResult confirmReceiptNormal(Integer userId, String bigOrderCode, String childOrderCode, boolean isSelf) {
        if (userId == null) {
            return RespUtil.unlogin();
        }
        LitemallOrderItem orderItem = litemallOrderService.findItemBySn(userId, bigOrderCode, childOrderCode);
        return confirmReceiptNormal(orderItem, isSelf);
    }

    /**
     * 确认收货前判断物流是否完成
     *
     * @return
     */
    public boolean isFinishSubOrderLogistics(String bigOrderSn, Integer subOrderId) {
        AgedmallLgBaseInfoExample baseInfoExample = new AgedmallLgBaseInfoExample();
        baseInfoExample.or().andOrderCodeEqualTo(bigOrderSn);
        List<AgedmallLgBaseInfo> agedmallLgBaseInfos = agedmallLgBaseInfoMapper.selectByExampleWithBLOBs(baseInfoExample);
        for (int i = 0; i < agedmallLgBaseInfos.size(); i++) {
            List<Integer> orderGoodsIds = logisticBusiBaseServiceImpl.getorderGoodsIds(agedmallLgBaseInfos.get(i).getLgContent());
            logger.debug("确认订单获取的订单商品id {},{},{}", JSONObject.toJSONString(orderGoodsIds), bigOrderSn, subOrderId);
            LitemallOrderGoods orderGoods = litemallOrderGoodsMapper.selectByPrimaryKey(orderGoodsIds.get(0));
            logger.debug("确认订单获取的订单商品 {},{},{},{}", orderGoods.getChildOrderId(), agedmallLgBaseInfos.get(i).getLgIsOver()
                    , bigOrderSn, subOrderId);
            if (orderGoods.getChildOrderId().intValue() == subOrderId.intValue() && agedmallLgBaseInfos.get(i).getLgIsOver().equals(LgBaseInfoIsOverEnum.END.getCode())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 自提确认收货
     * @param ids 子订单id列表
     * @return
     */
    public void confirmReceiptSelf(List<Integer> ids) throws AgedmallException {
        List<LitemallOrderItem> orderItemList = litemallOrderService.findItemListByIds(ids);
        List<String> bigOrderCodeList = orderItemList.stream().map(LitemallOrderItem::getOrderCode).collect(Collectors.toList());
        AgedmallLgBaseInfoExample baseInfoExample = new AgedmallLgBaseInfoExample();
        baseInfoExample.createCriteria().andOrderCodeIn(bigOrderCodeList);
        List<AgedmallLgBaseInfo> baseInfoList = agedmallLgBaseInfoMapper.selectByExampleWithBLOBs(baseInfoExample);
        Map<String, List<AgedmallLgBaseInfo>> baseInfoMap = baseInfoList.stream().collect(Collectors.groupingBy(AgedmallLgBaseInfo::getOrderCode));
        for(LitemallOrderItem orderItem: orderItemList) {
            String returnType = "";
            List<LitemallOrderGoods> orderGoodsList = litemallOrderGoodsService.getOrderGoodsListByChildOrderCode(orderItem.getChildOrderCode());
            LitemallOrderGoods orderGoods = orderGoodsList.get(0);
            //到店 自提
            if (orderItem.getDeliverySn().equals(AgedmallShipDictConstant.SHIP_SN_SELF)) {
                if(orderGoods.getStockType()==1){
                    //有软链锁
                    returnType = OrderSelfTypeEnum.STORE_LOCK.getType();
                } else{
                    //无软链锁
                    returnType = OrderSelfTypeEnum.STORE_BASE.getType();
                }
            } else if (orderItem.getDeliverySn().equals(AgedmallShipDictConstant.SHIP_SN_HOSPITAL)) {
                //到院自提
                JSONObject jsonObject = JSON.parseObject(orderItem.getDeliveryChoose());
                String type = (String) jsonObject.get("type");
                if (type.equals("subHospitalSelf")) {
                    if (orderGoodsList.get(0).getHospitalSelfLocationId() != null && orderGoodsList.get(0).getLocationHpType() == 0) {
                        returnType = OrderSelfTypeEnum.HOSPITAL_BASE.getType();
                    } else {
                        returnType = OrderSelfTypeEnum.HOSPITAL_LOCK.getType();
                    }
                }
            }
            if(StringUtils.isBlank(returnType)) {
                throw new AgedmallException(ExceptionMsgEnum.ORDER_CONFIRM_RECEIVE_SELF_TYPE_NULL);
            }
            if(OrderSelfTypeEnum.STORE_LOCK.getType().equals(returnType) || OrderSelfTypeEnum.HOSPITAL_LOCK.getType().equals(returnType)) {
                //有锁具
                lockSelfConfirm(orderItem.getUserId(),orderGoods.getRfid() ,orderItem.getChildOrderCode());
            }else {
                //自提码
                List<AgedmallLgBaseInfo> bList = baseInfoMap.get(orderItem.getOrderCode());
                int baseInfoId = bList.size() > 0 ? bList.get(0).getId().intValue() : 0;
                if(baseInfoId == 0) {
                    throw new AgedmallException(ORDER_CONFIRM_RECEIVE_SELF_BASE_ID_NULL);
                }
                String selfCode = stringRedisTemplate.opsForValue().get("self-" + baseInfoId + "-code");
                logger.debug("订单确认收货，无锁具自提码:{},{}",orderItem.getChildOrderCode(), selfCode);
                //无锁具
                SelfConfirmVo selfConfirmVo = new SelfConfirmVo();
                selfConfirmVo.setBigOrderCode(orderItem.getOrderCode());
                selfConfirmVo.setChildOrderCode(orderItem.getChildOrderCode());
                selfConfirmVo.setExtractCode(selfCode);
                Integer userId = orderItem.getUserId();
                selfConfirm(selfConfirmVo, userId);
            }
        }
    }

    /**
     * 链条锁自提确认收货
     * @param userId
     * @param equRfid
     * @param childOrderCode
     * @return
     */
    public Object lockSelfConfirm(Integer userId, String equRfid,  String childOrderCode) {
            if(StringUtils.isNotBlank(childOrderCode)){
                LitemallOrderItemExample orderItemExample = new LitemallOrderItemExample();
                orderItemExample.createCriteria().andChildOrderCodeEqualTo(childOrderCode).andUserIdEqualTo(userId);
                LitemallOrderItem orderItem = litemallOrderItemMapper.selectOneByExampleSelective(orderItemExample);
                return confirmReceiptNormal(userId, orderItem.getOrderCode(), childOrderCode, true);
            } else{
                //兼职人员进行维护
                AgedmallLgRfidGoodsCurStatExample goodsCurStatExample=new AgedmallLgRfidGoodsCurStatExample();
                goodsCurStatExample.or().andTypeEqualTo(1).andRfidEqualTo(equRfid);
                AgedmallLgRfidGoodsCurStat curStat = agedmallLgRfidGoodsCurStatMapper.selectOneByExample(goodsCurStatExample);
                curStat.setCurStat(11);
                AgedmallGoodsEquExample goodsEquExample = new AgedmallGoodsEquExample();
                goodsEquExample.or().andTypeEqualTo(0).andEquIdEqualTo(Long.parseLong(equRfid)).andDeletedEqualTo((byte) 0);
                AgedmallGoodsEqu goodsEqu = new AgedmallGoodsEqu();
                goodsEqu.setState(2);
                agedmallLgRfidGoodsCurStatMapper.updateByPrimaryKeySelective(curStat);
                agedmallGoodsEquMapper.updateByExampleSelective(goodsEqu,goodsEquExample);
                return RespUtil.success();
            }
    }

    /**
     * 无锁具自提确认收货
     * @param selfConfirmVo
     * @param userId
     * @return
     */
    public Object selfConfirm(SelfConfirmVo selfConfirmVo, Integer userId) {
        if (userId == null) {
            return RespUtil.unlogin();
        }
        LitemallOrder bigOrder = litemallOrderService.findBySn(userId, selfConfirmVo.getBigOrderCode());
        if (null == bigOrder) {
            return RespUtil.badArgumentValue();
        }
        LitemallOrderItem order = litemallOrderService.findItemBySn(userId, bigOrder.getOrderSn(), selfConfirmVo.getChildOrderCode());
        if (null == order) {
            return RespUtil.badArgumentValue();
        }

        //手动输入验证码
        if (StrUtil.isNotBlank(selfConfirmVo.getExtractCode())) {
            AgedmallLgBaseInfoExample baseInfoExample = new AgedmallLgBaseInfoExample();
            baseInfoExample.or().andOrderCodeEqualTo(selfConfirmVo.getBigOrderCode());
            LitemallOrderGoodsExample orderGoodsExample = new LitemallOrderGoodsExample();
            orderGoodsExample.or().andChildOrderIdEqualTo(order.getId().longValue());
            List<LitemallOrderGoods> orderGoodsList = litemallOrderGoodsMapper.selectByExample(orderGoodsExample);
            List<Integer> orderGoodsIdList = new ArrayList<>();
            orderGoodsList.forEach(orderGoods -> orderGoodsIdList.add(orderGoods.getId()));
            List<AgedmallLgBaseInfo> agedmallLgBaseInfos = agedmallLgBaseInfoMapper.selectByExampleWithBLOBs(baseInfoExample);
            for (int i = 0; i < agedmallLgBaseInfos.size(); i++) {
                List<Integer> integers = logisticBusiBaseServiceImpl.getorderGoodsIds(agedmallLgBaseInfos.get(i).getLgContent());
                for (int j = 0; j < integers.size(); j++) {
                    for (int k = 0; k < orderGoodsIdList.size(); k++) {
                        if (integers.get(j).equals(orderGoodsIdList.get(k))) {
                            String selfCode = stringRedisTemplate.opsForValue().get("self-" + agedmallLgBaseInfos.get(i).getId().toString() + "-code");
                            if (StrUtil.isBlank(selfCode)) {
                                return RespUtil.fail(500, "验证码已过期");
                            } else if (!selfCode.equals(selfConfirmVo.getExtractCode())) {
                                return RespUtil.fail(500, "验证码错误");
                            } else if (selfCode.equals(selfConfirmVo.getExtractCode())) {
                                //确认收货操作
                                return confirmReceiptNormal(userId, selfConfirmVo.getBigOrderCode(), selfConfirmVo.getChildOrderCode(), true);
                            }
                        }
                    }
                }
            }
            return RespUtil.fail(500, "找不到对应的物流订单");
        }
        //扫描打印码
        else if (StrUtil.isNotBlank(selfConfirmVo.getPrinterCode())) {
            String[] split = selfConfirmVo.getPrinterCode().split("-");
            if (split.length < 2) {
                return RespUtil.fail(1011, "打印包裹码格式错误");
            }
            AgedmallLgMission lgMission = agedmallLgMissionMapper.selectByPrimaryKey(Integer.parseInt(split[1]));
            //新增的小程序二维码扫描，走验证码
            if(ObjectUtil.isNull(lgMission)){
                SelfConfirmVo toCodeVo=new SelfConfirmVo();
                toCodeVo.setBigOrderCode(selfConfirmVo.getBigOrderCode());
                toCodeVo.setChildOrderCode(selfConfirmVo.getChildOrderCode());
                toCodeVo.setExtractCode(split[1]);
                selfConfirm(toCodeVo,userId);
            }
            if (lgMission.getMissionIsOver() != 2) {
                return RespUtil.fail(500, "商家未完成配送");
            }
            AgedmallLgBaseInfo baseInfo = agedmallLgBaseInfoMapper.selectByPrimaryKey(lgMission.getBaseInfoId().longValue());
            List<Integer> integers = logisticBusiBaseServiceImpl.getorderGoodsIds(baseInfo.getLgContent());
            LitemallOrderGoodsExample orderGoodsExample = new LitemallOrderGoodsExample();
            orderGoodsExample.or().andIdIn(integers);
            List<LitemallOrderGoods> orderGoodsList = litemallOrderGoodsMapper.selectByExample(orderGoodsExample);
            if (orderGoodsList.get(0).getChildOrderId().intValue() != order.getId()) {
                return RespUtil.fail(500, "二维码错误-不是当前订单");
            }
            //确认收货操作
            return confirmReceiptNormal(userId, selfConfirmVo.getBigOrderCode(), selfConfirmVo.getChildOrderCode(), true);
        }
        return RespUtil.fail(500, "自提订单确认失败");
    }


}

